package com.wt.struggle.goodbye.user.activity

import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.sq.activity.PickDetailsActivity
import com.wt.struggle.goodbye.user.adapter.ManagerAdapter
import kotlinx.android.synthetic.main.friend_manager_layout.*
import kotlinx.android.synthetic.main.head.*

/**
 * 圈子管理
 */
class FriendManagerActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.GET_SHE_QU_CODE -> {
                refreshView.isRefreshing = false

                if (netInfo.code == Config.SUCCESS) {

                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "") {

                        page++

                        val arr = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )
                        adapter.updateDataClear(arr)
                    }
                }
            }

            Send.COLLECT_CODE -> {

                indexNum++

                log("indexNum-----$indexNum")

                if (indexNum == idStr.size) {
                    showToastShort("取消成功")
                    page = 1
                    get()
                    removeLoadDialog()
                }

            }
        }

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.friend_manager_layout)
        tvTitle.text = "圈子管理"
        imageBack.setOnClickListener {
            finish()
        }

        imageCycleDelete.setOnClickListener {
            deleteCycle()
        }

        refreshView.setOnRefreshListener {
            page = 1
            get()
        }

        refreshView.onLoadMore(recyclerView) {
            get()
        }

        initAdapter()
        get()
    }


    val idStr = arrayListOf<String>()
    var indexNum = 0

    /**
     * 删除圈子
     */
    private fun deleteCycle() {

        idStr.clear()
        indexNum = 0

        for (temp in list) {
            if (temp.isCheck) {
                idStr.add(temp.id.toString())
            }
        }

        for (ss in idStr) {
            send!!.collect(ss, 2, handler)
        }

        showLoadDialog("取消中")

    }

    var page = 1
    fun get() {
        send!!.get(page, handler)
    }

    lateinit var adapter: ManagerAdapter
    val list = arrayListOf<NewsInfo>()

    private fun initAdapter() {
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        adapter = ManagerAdapter(this, list)
        recyclerView.adapter = adapter
        adapter.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {
                val intent = Intent(this@FriendManagerActivity, PickDetailsActivity::class.java)
                intent.putExtra("info", list[position])
                intent.putExtra("code", 1)
                startActivity(intent)

            }

            override fun onLongClick(position: Int) {

            }

        }


    }
}